package com.mage.myshop.controller;

import com.mage.myshop.model.Category;
import com.mage.myshop.service.CategoryService;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

@Controller
public class IndexController {

    @Resource
    private freemarker.template.Configuration configuration;

    @RequestMapping("generate")
    @ResponseBody
    public String generate() throws IOException {
        // 先获取模板
        Template template = configuration.getTemplate("index.ftl");
        // 获取index.html输出路径
        String filePath = IndexController.class.getResource("/").getPath() + "static/index.html";
        // 构建FileWriter流
        FileWriter out =  new FileWriter(filePath);
        try {
            template.process(null, out);
        } catch (TemplateException e) {
            return "生成失败。";
        }
        return "success";
    }

}
